package com.ruoyi.web.controller.expert;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.expert.domain.ExpertPestControl;
import com.ruoyi.expert.service.FileService;
import com.ruoyi.expert.service.IExpertPestControlService;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 病虫害防治Controller
 *
 * @author ruoyi
 * @date 2022-03-10
 */
@RestController
@RequestMapping("/system/control")
public class ExpertPestControlController extends BaseController
{
    @Autowired
    private IExpertPestControlService expertPestControlService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private FileService fileService;

    /**
     * 查询病虫害防治列表
     */
//    @PreAuthorize("@ss.hasPermi('system:control:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExpertPestControl expertPestControl)
    {
        startPage();
        List<ExpertPestControl> list = expertPestControlService.selectExpertPestControlList(expertPestControl);
        return getDataTable(list);
    }


    /**
     * 获取病虫害防治详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:control:query')")
    @GetMapping(value = "/{expertdbId}")
    public AjaxResult getInfo(@PathVariable("expertdbId") Long expertdbId)
    {
        return AjaxResult.success(expertPestControlService.selectExpertPestControlByExpertdbId(expertdbId));
    }

    /**
     * 新增病虫害防治
     */
//    @PreAuthorize("@ss.hasPermi('system:control:add')")
    @Log(title = "病虫害防治", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExpertPestControl expertPestControl, HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        expertPestControl.setCreateBy(loginUser.getUser().getUserName());
        expertPestControlService.insertExpertPestControl(expertPestControl);
        return AjaxResult.success(expertPestControl.getExpertdbId());
    }

    /**
     * 修改病虫害防治
     */
//    @PreAuthorize("@ss.hasPermi('system:control:edit')")
    @Log(title = "病虫害防治", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExpertPestControl expertPestControl)
    {
        return toAjax(expertPestControlService.updateExpertPestControl(expertPestControl));
    }

    /**
     * 删除病虫害防治
     */
//    @PreAuthorize("@ss.hasPermi('system:control:remove')")
    @Log(title = "病虫害防治", businessType = BusinessType.DELETE)
	@DeleteMapping("/{expertdbIds}")
    public AjaxResult remove(@PathVariable Long[] expertdbIds)
    {
        for (Long expertdbId : expertdbIds) {
            fileService.deleteWordFile(Integer.parseInt(expertdbId.toString()), "expert_pest_controller");
            fileService.deleteWordFile(Integer.parseInt(expertdbId.toString()),"expert_pest_controller");
        }
        return toAjax(expertPestControlService.deleteExpertPestControlByExpertdbIds(expertdbIds));
    }
}
